package com.fau.wmbj.server.storage;

import com.fau.wmbj.server.utils.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.InputStream;

/**
 * fastdfs文件系统插件
 *
 * @author jiawei
 * @version 1.0
 * @datetime 2019/12/23 21:52
 **/
@Component("fastDFSStorage")
public class FastDFSStorage extends Storage{

    @Override
    public String getName() {
        return "FastDFS存储";
    }


    @Override
    public String upload(String path, File file, String contentType, boolean open) {
        //fastdfs上传的文件的路径是随机生成的，所以上传后必须返回path
        return null;
    }

    /**
     * 文件上传
     *
     * @param path          上传路径
     * @param multipartFile 上传文件
     * @return String 文件路径
     */
    @Override
    public String upload(String path, MultipartFile multipartFile, boolean open) throws Exception {
        String fastDFSGroup = getAttribute("fastDFSGroup");
        //StorePath storePath = storageClient.uploadFile(fastDFSGroup, multipartFile.getInputStream(), multipartFile.getSize(), FilenameUtils.getExtension(multipartFile.getOriginalFilename()));
        //return storePath.getPath();
        return "";
    }

    @Override
    public void delete(StorageDirectory fileDetail, boolean isPreview) {
        String url = isPreview ? fileDetail.getPreview() : fileDetail.getUrl();
        String groupId = fileDetail.getGroupId();
        if (StringUtils.isNotBlank(url) && StringUtils.isNotBlank(groupId)) {
            //storageClient.deleteFile(groupId, url);
        }
    }

    /**
     * 文件删除
     *
     * @param url     文件路径
     * @param groupId 存储桶
     */
    @Override
    public void delete(String url, String groupId) {
        if (StringUtils.isNotBlank(url) && StringUtils.isNotBlank(groupId)) {
            //storageClient.deleteFile(groupId, url);
        }
    }

    @Override
    public String getUrl(StorageDirectory fileDetail, boolean isPreview, int timeOut) {
        String url = isPreview ? fileDetail.getPreview() : fileDetail.getUrl();
        return fileDetail.getStorageServer() + "/" + fileDetail.getGroupId() + "/" + url;
    }

    /**
     * 获取服务端存储路径
     *
     * @param diskPath 上传路径
     * @return 访问URL
     */
    @Override
    public String getStoragePath(String diskPath) {
        return diskPath;
    }

    /**
     * 下载文件
     *
     * @param fileDetail 文件详情
     * @author jiawei
     * @date 2020-08-16 21:43
     */
    @Override
    public InputStream download(StorageDirectory fileDetail, boolean isPreview) {
        //DownloadByteArray callback = new DownloadByteArray();
        //String url = isPreview ? fileDetail.getPreview() : fileDetail.getUrl();
        //byte[] bytes = storageClient.downloadFile(fileDetail.getGroupId(), url, callback);
        //return bytes;
        return null;
    }
}
